xxxxxxxxxx
60
/*
----- Sound Mini Series by Patt Vira -----
Name: EP5_p5.FFT - Class Demo
Video Tutorial: https://youtu.be/8O5aCwdopLo
Connect with Patt: @pattvira
https://www.pattvira.com/
----------------------------------------
*/
let song;
let fft;
let waveform = []; let smoothing = 0.8; let bins = 512;
let spectrum = []; let r = 100;
function preload() {
song = loadSound("Veiil.mp3");
}
function setup() {
createCanvas(bins, 400);
song.play();
fft = new p5.FFT(smoothing, bins);
}
function draw() {
background(220);
waveform = fft.waveform();
spectrum = fft.analyze();
let vol = fft.getEnergy(20, 140);
stroke(255);
line(0, height/2, width, height/2);
if (vol > 250) {
stroke(255, 0, 0);
} else {
stroke(0);
}
// Frequency Domain
for (let i=0; i<spectrum.length; i++) {
let y = map(spectrum[i], 0, 255, 0, height/2);
line(i, height, i, height - y);
}
//Time Domain
for (let i=0; i<waveform.length; i++) {
let y = height/4 + map(waveform[i], -1, 1, -r, r);
ellipse(i, y, 1, 1);
}
}